//
//  WBBaseViewController.swift
//  XLWB
//
//  Created by MacBook on 16/6/29.
//  Copyright © 2016年 MacBook. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController {

    //用户登录标记
    var userLogon = true
    //访客视图信息字典
    var visitorInfoDictionary: [String: String]?
    
    var tableView: UITableView?
    //刷新控件
    var refreshControl: CDRefreshControl?
    //上拉刷新标记
    var isPullup = false
    
    //自定义导航条
    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0,y: 0,width:  UIScreen.cz_screenWidth(),height:  64))
    //自定义导航条目
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()

      setupUI()
     
        WBNetworkManager.shared.userLogon ? loadData() : ()
        
        //注册登录成功通知
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(loginSuccess),
            name: NSNotification.Name(rawValue: WBUserLoginSuccessedNotification),
            object: nil)
    }
    
    deinit {
        //注销通知
        NotificationCenter.default.removeObserver(self)
    }
    override var title: String? {
    
        didSet {
        navItem.title = title
        
        
        }
    
    }
    
    //加载数据
    func loadData() {
    
    //如果子类不识闲任何办法,默认关闭刷新控件
        refreshControl?.endRefreshing()
    }
    
}


//MARK:--UITableViewDataSource,UITableViewDelegate
extension WBBaseViewController: UITableViewDataSource,UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 10
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        
        let section = tableView.numberOfSections - 1
        
        if row < 0 || section < 0 {
            return
        }
        
        let count = tableView.numberOfRows(inSection: section)
        
        if row == (count - 1) && !isPullup {
            
//            print("上拉刷新")
            isPullup = true
            
            //刷新
            loadData()
        }
    }
}
//MARK--访客监听方法
extension WBBaseViewController {

    //登录成功处理
    @objc private func loginSuccess(n: Notification) {
    
        print("登录成功")
        
        //登录前左边是注册,右边是登录
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        
        view = nil
        
        NotificationCenter.default.removeObserver(self)
    
    }
    
    
    
    @objc private func login() {
    
        print("用户登录")
        
        //发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBUserShouldLoginNotification), object: nil)
    }


    @objc private func register() {
    
    
    print("用户注册")
    
    }


}

//MARK--设置界面
extension WBBaseViewController {

    
    
   private func setupUI() {
    
        view.backgroundColor = UIColor.white()
        setupNavigationBar()
//        setupTableView()
        automaticallyAdjustsScrollViewInsets = false

//        userLogon ? setupTableView() : setupVisitorView()
    WBNetworkManager.shared.userLogon ? setupTableView() : setupVisitorView()
}

     func setupTableView() {
    
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: navigationBar)
    
        tableView?.dataSource = self
        tableView?.delegate = self
        tableView?.contentInset = UIEdgeInsetsMake(navigationBar.bounds.height,
            0,
            tabBarController?.tabBar.bounds.height ?? 49,
            0)
        
        //修改指示器的缩进
        tableView?.scrollIndicatorInsets = tableView!.contentInset
        
        refreshControl = CDRefreshControl()
        tableView?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    
    }
    
    //MARK--访客视图
    func setupVisitorView() {
    
        let visitorView = WBVisitorView(frame: view.bounds)
        view.insertSubview(visitorView, belowSubview: navigationBar)
    
        visitorView.visitorInfo = visitorInfoDictionary
    
        visitorView.loginBtn.addTarget(self, action: #selector(login), for: .touchUpInside)
        visitorView.registerBtn.addTarget(self, action: #selector(register), for: .touchUpInside)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(login))


    }
    
    
    //设置导航条
    func setupNavigationBar() {
        
        view.addSubview(navigationBar)
        navigationBar.items = [navItem]
        
        //设置navBar渲染颜色
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray()]
        
        //设置系统按钮文字的颜色
        navigationBar.tintColor = UIColor.orange()
        
        
    }
}
